package com.example.children.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.children.DTO.AdminSelect;
import com.example.children.Exception.StudentException;
import com.example.children.FileService.studentInterface;
import com.example.children.Mapper.StudentMapper;
import com.example.children.Mapper.UserMapper;
import com.example.children.Model.Student;
import com.example.children.DTO.StudentClassDTO;
import com.example.children.DTO.TeacherSelect;
import com.example.children.Model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * StudentService 服务层
 */
@Service
public  class StudentService extends ServiceImpl<UserMapper, User> implements studentInterface {

    @Autowired
    private StudentMapper studentMapper;

    // 插入一个新的
    public void insertStudent(Student student) {
        studentMapper.insert(student);
    }

    // 添加分页查询
    public Page<StudentClassDTO> getStudentWithClassPage(int pageNo, int pageSize,String teacherId) {
        // 创建分页对象
        Page<StudentClassDTO> page = new Page<>(pageNo, pageSize);

        // 通过 MyBatis-Plus 的分页查询
        Page<StudentClassDTO> studentPage = studentMapper.selectStudentWithClassPage(page,teacherId);

        return studentPage;
    }


    // 查询所有学生
    public List<Student> getAllStudents() {
        return studentMapper.selectList(null);  // 查询所有数据
    }

    //查询所有学生和班级信息
    public List<StudentClassDTO> getStudentWithClass() {
        System.out.println("查询所有学生和班级信息");
        List<StudentClassDTO> selectStudentClassDTO= studentMapper.selectStudentClassDTO();
        if(selectStudentClassDTO==null){
            throw new StudentException("没有学生数据");
        }
        else
            return selectStudentClassDTO;
    }

    // 根据 id 查询学生
    public Student getStudentById(String id) {
        return studentMapper.selectToId(id);
    }

    // 根据 id 更新学生信息
    public void updateStudent(Student student) {
        studentMapper.updateById(student);
    }

    // 根据 id 删除学生
    public void deleteStudent(String id) {
        studentMapper.deleteById(id);
    }


    //查询学生入园日期
    public List<Student> selectByDateStudent(Student student) {
        return studentMapper.selectByDateSend(student);
    }


    //区间范围内的日期查询
    public List<Student> age(TeacherSelect teacherSelect) {
        return studentMapper.findStudents(teacherSelect);
    }

    //管理员查询学生
    public List<Student> AdminSelectStudent(AdminSelect adminSelect){
        return studentMapper.findStudentsAdmin(adminSelect);
    }


//admin
    public Page<StudentClassDTO> getStudentWithClassPageAdmin(int pageNo, int pageSize) {
        // 创建分页对象
        Page<StudentClassDTO> page = new Page<>(pageNo, pageSize);

        // 通过 MyBatis-Plus 的分页查询
        Page<StudentClassDTO> studentPage = studentMapper.getStudentWithClassPageAdmin(page);

        return studentPage;
    }

    // 方法来获取分页数据
    public Page<StudentClassDTO> getStudentWithClassPage(int pageNo, int pageSize) {
        // 计算分页的偏移量
        int offset = (pageNo - 1) * pageSize;

        // 获取当前页的数据
        List<StudentClassDTO> records = studentMapper.getStudentWithClassPageAdminSHOUDONG(offset, pageSize);
        // 获取总记录数
        int total = studentMapper.countTotalRecords();
        // 构造并返回分页对象
        Page<StudentClassDTO> page = new Page<>();
        page.setTotal(total);
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        page.setRecords(records);

        return page;
    }


}
